package com.ymatou.iapi.trading.userquery.testcase;

import java.math.BigDecimal;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.ymatou.iapi.optrade.parameter.enums.CancelOrderTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymatou.iapi.trading.userquery.parameter.GetPayOrderCountRequest;
import com.ymatou.iapi.trading.userquery.service.GetPayOrderCountCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.common.order.userquery.util.OrderHelper;
import com.ymttest.database.mongo.UserIndexQueryWapper;
import com.ymttest.database.mongo.config.UserIndexField;

public class Ts_GetPayOrderCount {
	
	private static GetPayOrderCountRequest getPayOrderCountRequest;
	private static GetPayOrderCountCall getPayOrderCountCall;
	
	private static Integer userId = 20238699;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取买家在某买手下支付过的订单数");
	}

	@Before
	public void caseUp() {
		getPayOrderCountRequest = new GetPayOrderCountRequest();
		getPayOrderCountCall = new GetPayOrderCountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getPayOrderCountCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void verify() throws JSONException {
		
		int expectCount = getPayOrderCountCall.getData().getInt("count");
		boolean expectSuccess = getPayOrderCountCall.getData().getBoolean("success");
		
		Criteria criatira = new Criteria().andOperator(
				Criteria.where(UserIndexField.USER_ID).is(
						getPayOrderCountRequest.getBuyerId()),
				Criteria.where(UserIndexField.SELLER_ID).is(
						getPayOrderCountRequest.getSellerId()),
				Criteria.where(UserIndexField.PAID_TIME).ne(null),
				Criteria.where(UserIndexField.ORDER_TYPE).nin(
						OrderHelper.getMemberOrderTypes()));
		
		long count = new UserIndexQueryWapper().count(new Query(criatira),
				getPayOrderCountRequest.getBuyerId());
		
		Logger.verifyEquals(expectCount, (int)count, "订单数量验证");
		Logger.verifyEquals(true, expectSuccess, "接口是否正确返回");
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPayOrderCount_001() {
		Logger.start(true, "当前买家在该买手下单，并没有支付");
		try {
			
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);


			long userId = placeOrder.getUserId();

			long sellerId = placeOrder.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");
			
			getPayOrderCountRequest.setBuyerId(userId);
			getPayOrderCountRequest.setSellerId(sellerId);

			getPayOrderCountCall.setData(getPayOrderCountRequest);
			getPayOrderCountCall.callService();
			
			Logger.verifyEquals(200, getPayOrderCountCall.getCode(), "校验code");
			Logger.verifyEquals(0, getPayOrderCountCall.getBCode(), "校验bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPayOrderCount_002() {
		Logger.start(true, "当前买家在该买手下单，已经支付");
		try {
			
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);


			long userId = placeOrder.getUserId();
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();
			BigDecimal useAccountAmount = placeOrder.getPayableAmount();
			long tradingId = placeOrder.getTradingId();
			
			TradingCallService.payOrder(userId, useAccountAmount, tradingId, false, 2);

			Logger.comment("前置数据准备结束");
			
			getPayOrderCountRequest.setBuyerId(userId);
			getPayOrderCountRequest.setSellerId(sellerId);

			getPayOrderCountCall.setData(getPayOrderCountRequest);
			getPayOrderCountCall.callService();
			
			Logger.verifyEquals(200, getPayOrderCountCall.getCode(), "校验code");
			Logger.verifyEquals(0, getPayOrderCountCall.getBCode(), "校验bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPayOrderCount_003() {
		Logger.start(true, "当前买家在该买手下单，但是订单被取消");
		try {
			
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);


			long userId = placeOrder.getUserId();
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();
			BigDecimal useAccountAmount = placeOrder.getPayableAmount();
			long tradingId = placeOrder.getTradingId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			
			TradingCallService.payOrder(userId, useAccountAmount, tradingId, false, 2);
			TradingCallService.cancelOrder(orderId, CancelOrderTypeEnum.SYSTEM_CANCEL_ORDER, userId);

			Logger.comment("前置数据准备结束");
			
			getPayOrderCountRequest.setBuyerId(userId);
			getPayOrderCountRequest.setSellerId(sellerId);

			getPayOrderCountCall.setData(getPayOrderCountRequest);
			getPayOrderCountCall.callService();
			
			Logger.verifyEquals(200, getPayOrderCountCall.getCode(), "校验code");
			Logger.verifyEquals(0, getPayOrderCountCall.getBCode(), "校验bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPayOrderCount_004() {
		Logger.start(true, "新买家在该买手下单，但是未支付");
		try {
			
			Logger.comment("前置数据准备");

			int userId = new OrderCallService().createNewUser();
			TradingServiceResp placeOrder = TradingPlaceOrderCallService.placeOrderWithUserId(userId);
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");
			
			getPayOrderCountRequest.setBuyerId(userId);
			getPayOrderCountRequest.setSellerId(sellerId);

			getPayOrderCountCall.setData(getPayOrderCountRequest);
			getPayOrderCountCall.callService();
			
			Logger.verifyEquals(200, getPayOrderCountCall.getCode(), "校验code");
			Logger.verifyEquals(0, getPayOrderCountCall.getBCode(), "校验bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPayOrderCount_005() {
		Logger.start(true, "新买家在该买手下单，已经支付");
		try {
			
			Logger.comment("前置数据准备");

			int userId = new OrderCallService().createNewUser();
			TradingServiceResp placeOrder = TradingPlaceOrderCallService.placeOrderWithUserId(userId);
			long sellerId = placeOrder.getOrderList().get(0).getSellerId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			BigDecimal useAccountAmount = placeOrder.getPayableAmount();
			long tradingId = placeOrder.getTradingId();
			
			TradingCallService.payOrder(userId, useAccountAmount, tradingId, false, 2);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");
			
			getPayOrderCountRequest.setBuyerId(userId);
			getPayOrderCountRequest.setSellerId(sellerId);

			getPayOrderCountCall.setData(getPayOrderCountRequest);
			getPayOrderCountCall.callService();
			
			Logger.verifyEquals(200, getPayOrderCountCall.getCode(), "校验code");
			Logger.verifyEquals(0, getPayOrderCountCall.getBCode(), "校验bcode");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPayOrderCount_006() {
		Logger.start(true, "userId为0");
		try {
			
			getPayOrderCountRequest.setBuyerId(0);
			getPayOrderCountRequest.setSellerId(24635724);

			getPayOrderCountCall.setData(getPayOrderCountRequest);
			getPayOrderCountCall.callService();
			
			Logger.verifyEquals(400, getPayOrderCountCall.getCode(), "校验code");
			Logger.verifyEquals(100, getPayOrderCountCall.getBCode(), "校验bcode");
			Logger.verifyEquals("buyerId:buyerId未提供",
					getPayOrderCountCall.getMsg(), "校验msg");
			Logger.verifyEquals("ILLEGAL_ARGUMENT", getPayOrderCountCall
					.getData().getString("errorCode"), "校验errorCode");
			Logger.verifyEquals("buyerId:buyerId未提供", getPayOrderCountCall
					.getData().getString("errorMessage"), "校验errorMessage");
			Logger.verifyEquals(false, getPayOrderCountCall.getData()
					.getBoolean("success"), "校验success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPayOrderCount_007() {
		Logger.start(true, "sellerId为0");
		try {
			
			getPayOrderCountRequest.setBuyerId(24787767);
			getPayOrderCountRequest.setSellerId(0);

			getPayOrderCountCall.setData(getPayOrderCountRequest);
			getPayOrderCountCall.callService();
			
			Logger.verifyEquals(400, getPayOrderCountCall.getCode(), "校验code");
			Logger.verifyEquals(100, getPayOrderCountCall.getBCode(), "校验bcode");
			Logger.verifyEquals("sellerId:sellerId未提供",
					getPayOrderCountCall.getMsg(), "校验msg");
			Logger.verifyEquals("ILLEGAL_ARGUMENT", getPayOrderCountCall
					.getData().getString("errorCode"), "校验errorCode");
			Logger.verifyEquals("sellerId:sellerId未提供", getPayOrderCountCall
					.getData().getString("errorMessage"), "校验errorMessage");
			Logger.verifyEquals(false, getPayOrderCountCall.getData()
					.getBoolean("success"), "校验success");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
